﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Documents.Common.Interfaces;
using NWISPas.Repository.NWIS.PAS;
using System.Xml;
using Documents.Common.Helpers;
using System.Web.Services.Protocols;

namespace NWISPas.Repository.Controllers
{
    public class PasController : Controller, IPasRepository
    {
        public ActionResult GetPatient(string patientIdentifiers)
        {
            NWIS.PAS.ReqPatientDemographicType request = new NWIS.PAS.ReqPatientDemographicType();
            request.MessageHeader = new NWISPas.Repository.NWIS.PAS.MessageHeader();
            request.MessageHeader = GetMessageHeader();
            request.ReqPatientDemographicBody = new NWISPas.Repository.NWIS.PAS.ReqPatientDemographicBody();
            request.ReqPatientDemographicBody.PatientDemographicsServiceRequestPatientDemographics = new NWIS.PAS.PatientDemographicsServiceRequestPatientDemographics();
            string[] identifiers = patientIdentifiers.Split(new char[] { ',' });
            request.ReqPatientDemographicBody.PatientDemographicsServiceRequestPatientDemographics.identifier = CreatePatientIdentifier(identifiers[1], identifiers[0]);
            request.ReqPatientDemographicBody.PatientDemographicsServiceRequestPatientDemographics.serviceType = "";
            GatewayService svc = new GatewayService();
            NWIS.PAS.RespPatientDemographicType response = svc.PatientDemographicRequestOperation(request);
            XmlDocument result = XmlHelper.Serialize(response, null, "patientdemographics");
            RemoveMessageHeader(ref result);
            return new ContentResult
            {
                Content = result.OuterXml,
                ContentType = "application/xml"
            };
        }

        private void RemoveMessageHeader(ref XmlDocument source)
        {
            XmlNode messageHeaderNode = source.DocumentElement["MessageHeader"];
            source.DocumentElement.RemoveChild(messageHeaderNode);
        }


        private PatientIdentifier CreatePatientIdentifier(string id, string domain)
        {
            PatientIdentifier result = new PatientIdentifier();
            result.identifierValue = new Identifier();
            result.identifierValue.domainForIdentifier = new Domain();
            result.identifierValue.domainForIdentifier.domainName = domain;
            result.identifierValue.domainForIdentifier.domainId = "";
            result.identifierValue.identifier = id;
            return result;
        }

        private MessageHeader GetMessageHeader()
        {
            MessageHeader result = new MessageHeader();
            result.messageDataSize = 0;
            result.messageDisplayStatus = new MessageDisplaySensitivityTypeFlag();
            result.messageDisplayStatus.MessageDisplaySensitivityType = MessageDisplaySensitivityType.sensitiveDoNotDisplay;
            result.messageName = "";
            result.messageRecordNumber = 0;
            result.messageSecurityState = "";
            return result;
        }

        public ActionResult GetPatients(string searchCriteria)
        {
            throw new NotImplementedException();
        }

        public ActionResult Discharges()
        {
            return GetPatientList(PatientListType.TodaysOutPatients);
        }

        public ActionResult InPatients()
        {
            return GetPatientList(PatientListType.TodaysInPatientDayCaseAttending);
        }

        private ActionResult GetPatientList(PatientListType listtype)
        { 
            string wardCode = Request["wardCode"];
            string dateFrom = Request["dateFrom"];
            string dateTo = Request["dateTo"];
            return GetPatientList(wardCode, dateFrom, dateTo, listtype);
        }

        private ActionResult GetPatientList(string wardcode, string dateFrom, string dateTo,PatientListType listtype)
        {
            string spec = "";
            
            RespPatientListCareProcessStatusType resp = null;
            ReqPatientCareProcessStatusType req = new ReqPatientCareProcessStatusType();
            PatientCareProcessStatusBody body = new PatientCareProcessStatusBody();
            body.PatientCareProcessStatusRequest = new PatientCareProcessStatusRequest();
            body.PatientCareProcessStatusRequest.ListType = listtype;
            body.PatientCareProcessStatusRequest.ListTypeSpecified = true;
            body.PatientCareProcessStatusRequest.HealthService = new HealthService();
            body.PatientCareProcessStatusRequest.HealthService.healthServiceSpeciality = spec;
            body.PatientCareProcessStatusRequest.HealthService.healthServiceName = "";
            body.PatientCareProcessStatusRequest.ServicePoint = new ServicePoint();
            body.PatientCareProcessStatusRequest.ServicePoint.servicePointName = wardcode;
            body.PatientCareProcessStatusRequest.ResponsiblePerson = new PersonName();
            body.PatientCareProcessStatusRequest.ResponsiblePerson.familyName = "";
            body.PatientCareProcessStatusRequest.ResponsiblePerson.givenNames = "";
            body.PatientCareProcessStatusRequest.ResponsiblePerson.nameType = new NWISPas.Repository.NWIS.PAS.Type();
            body.PatientCareProcessStatusRequest.ResponsiblePerson.nameType.domain = "";

            if (dateFrom != null && dateFrom.Trim() != string.Empty)
            {
                body.PatientCareProcessStatusRequest.DateFrom = DateTimeFormatter.ParseDateTimeAsDateTime(dateFrom);
                body.PatientCareProcessStatusRequest.DateFromSpecified = true;
            }

            if (dateTo != null && dateTo.Trim() != string.Empty)
            {
                body.PatientCareProcessStatusRequest.DateTo = DateTimeFormatter.ParseDateTimeAsDateTime(dateTo);
                body.PatientCareProcessStatusRequest.DateToSpecified = true;
            }

            req.PatientCareProcessStatusBody = body;
            req.MessageHeader = GetMessageHeader();

            GatewayService svc = new GatewayService();
            resp = svc.PatientListOperation(req);
            XmlDocument result = XmlHelper.Serialize(resp.RespPatientListCareProcessStatusBody, "In Patients", "Patients");
            return new ContentResult
            {
                Content = result.OuterXml,
                ContentType = "application/xml"
            };
        }

        
        
        public ActionResult Locations()
        {
            ReqGetEntityLookUpType type = new ReqGetEntityLookUpType();
            type.ReqGetEntityLookUpBody = new ReqGetEntityLookUpBody();
            type.ReqGetEntityLookUpBody.LookupRequest = new LookupRequest();
            type.ReqGetEntityLookUpBody.LookupRequest.EntityType = EntityType.Location;
            type.MessageHeader = new NWISPas.Repository.NWIS.PAS.MessageHeader();

            GatewayService svc = new GatewayService();
            RespGetEntityLookUpType resp = svc.LookupRequestOperation(type);
            XmlDocument result = XmlHelper.Serialize(resp.RespGetEntityLookUpBody.LookupResponse.EntityArray.Where(o => o.EntityCode != null).ToArray(), null, "Locations");
            return new ContentResult
            {
                Content = result.OuterXml,
                ContentType = "application/xml"
            };
        }
    }
}
